package com.youdianstar.app.common.bind.viewadapter.view;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.databinding.BindingAdapter;

import com.youdianstar.app.common.bind.command.BindingCommand;
import com.youdianstar.app.common.util.AntiShakeUtil;
import com.youdianstar.app.common.util.LogUtil;
import com.youdianstar.app.common.util.StrUtil;


public class ViewAdapter {
    //防重复点击间隔(秒)
    private static final int CLICK_INTERVAL = 1;
    //防重复点击间隔(毫秒)
    private static final int HM_INTERVAL = 500;

    /**
     * requireAll 是意思是是否需要绑定全部参数, false为否
     * View的onClick事件绑定
     * onClickCommand 绑定的命令,
     * isThrottleFirst 是否开启防止过快点击
     */
    @SuppressLint("CheckResult")
    @BindingAdapter(value = {"onClickCommand", "isThrottleFirst"}, requireAll = false)
    public static void onClickCommand(View view, final BindingCommand clickCommand, final boolean isThrottleFirst) {
        //1秒钟内只允许点击1次
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AntiShakeUtil.isInvalidClick(view,HM_INTERVAL))
                    return;
                if (clickCommand != null) {
                    clickCommand.execute();
                }
            }
        });
    }


    /**
     * 回调控件本身
     *
     * @param currentView
     * @param bindingCommand
     */
    @BindingAdapter(value = {"currentView"}, requireAll = false)
    public static void replyCurrentView(View currentView, BindingCommand bindingCommand) {
        if (bindingCommand != null) {
            bindingCommand.execute(currentView);
        }
    }

    /**
     * view是否需要获取焦点
     */
    @BindingAdapter({"requestFocus"})
    public static void requestFocusCommand(View view, final Boolean needRequestFocus) {
        if (needRequestFocus) {
            view.setFocusableInTouchMode(true);
            view.requestFocus();
        } else {
            view.clearFocus();
        }
    }

    /**
     * view的焦点发生变化的事件绑定
     */
    @BindingAdapter({"onFocusChangeCommand"})
    public static void onFocusChangeCommand(View view, final BindingCommand<Boolean> onFocusChangeCommand) {
        view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (onFocusChangeCommand != null) {
                    onFocusChangeCommand.execute(hasFocus);
                }
            }
        });
    }

    /****************************************换肤根据配置加载资源***************************************************/

    /**
     * 设置背景颜色
     *
     * @param view 目标控件
     * @param color 默认资源
     * @param tag 额外配置标签,可以通过代码替换资源
     */
    @BindingAdapter(value = {"loadBgColor","tag"}, requireAll = false)
    public static void setBackgroundColor(View view,int color,String tag){
        if (!StrUtil.isEmpty(tag)){
            LogUtil.i("----loadTag:"+tag);
        }
        view.setBackgroundColor(color);
    }

    /**
     * 设置背景
     *
     * @param view 目标控件
     * @param drawable 默认资源
     */
    @BindingAdapter(value = {"loadBgDrawable","tag"}, requireAll = false)
    public static void setBackgroundDrawable(View view, Drawable drawable,String tag){
        if (!StrUtil.isEmpty(tag)){
            LogUtil.i("----loadTag:"+tag);
        }
        view.setBackground(drawable);
    }

    /**
     * 设置背景
     *
     * @param view 目标控件
     * @param source 默认资源
     */
    @BindingAdapter(value = {"loadBgResource","tag"}, requireAll = false)
    public static void setBackgroundResource(View view, int source,String tag){
        if (!StrUtil.isEmpty(tag)){
            LogUtil.i("----loadTag:"+tag);
        }
        view.setBackgroundResource(source);
    }

    @BindingAdapter("visibilityAmount")
    public static void setVisibilityAmount(View view, Double amount){
        boolean isVisible = (null != amount && amount > 0.0);
        view.setVisibility(isVisible?View.VISIBLE:View.GONE);
    }


}
